package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.vo.FlightOrderQtListVO;
import com.ktgj.flight.domain.vo.FlightOrderQtVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.FlightOrderQt;
import com.ktgj.flight.service.IFlightOrderQtService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 航班信息变动Controller
 * 
 * @author ruoyi
 * @date 2022-06-21
 */
@RestController
@RequestMapping("/orderQt")
public class FlightOrderQtController extends BaseController
{
    @Autowired
    private IFlightOrderQtService flightOrderQtService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询航班信息变动列表
     */
    @PreAuthorize(hasPermi = "flight:qt:list")
    @GetMapping("/list")
    public TableDataInfo list(FlightOrderQt flightOrderQt)
    {
        startPage();
        List<FlightOrderQt> list = flightOrderQtService.selectFlightOrderQtList(flightOrderQt);
        return getDataTable(list);
    }

    /**
     * 导出航班信息变动列表
     */
    @PreAuthorize(hasPermi = "flight:qt:export")
    @Log(title = "航班信息变动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightOrderQt flightOrderQt) throws IOException
    {
        List<FlightOrderQt> list = flightOrderQtService.selectFlightOrderQtList(flightOrderQt);
        ExcelUtil<FlightOrderQt> util = new ExcelUtil<FlightOrderQt>(FlightOrderQt.class);
        util.exportExcel(response, list, "航班信息变动数据");
    }

    /**
     * 获取航班信息变动详细信息
     */
    @PreAuthorize(hasPermi = "flight:qt:query")
    @GetMapping(value = "/{qtid}")
    public AjaxResult getInfo(@PathVariable("qtid") Long qtid)
    {
        return AjaxResult.success(flightOrderQtService.selectFlightOrderQtById(qtid));
    }

    /**
     * 新增航班信息变动
     */
    @PreAuthorize(hasPermi = "flight:qt:add")
    @Log(title = "航班信息变动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightOrderQt flightOrderQt)
    {
        return toAjax(flightOrderQtService.insertFlightOrderQt(flightOrderQt));
    }

    /**
     * 修改航班信息变动
     */
    @PreAuthorize(hasPermi = "flight:qt:edit")
    @Log(title = "航班信息变动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightOrderQt flightOrderQt)
    {
        return toAjax(flightOrderQtService.updateFlightOrderQt(flightOrderQt));
    }

    /**
     * 删除航班信息变动
     */
    @PreAuthorize(hasPermi = "flight:qt:remove")
    @Log(title = "航班信息变动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{qtids}")
    public AjaxResult remove(@PathVariable Long[] qtids)
    {
        return toAjax(flightOrderQtService.deleteFlightOrderQtByIds(qtids));
    }

    /**
     * 航班变动信息管理列表
     * @param flightOrderQt
     * @return
     */
    @PostMapping("/flightOrderQtList")
    public TableDataInfo flightOrderQtList(@RequestBody FlightOrderQtVO flightOrderQt) {
        startPage();
        List<FlightOrderQtListVO> orderQtList = flightOrderQtService.flightOrderQtList(flightOrderQt);
        return getDataTable(orderQtList);
    }

    /**
     * 延误信息处理-个人
     * @param map
     * @return
     */
    @PostMapping("/flightOrderQt")
    public AjaxResult flightOrderQt(@RequestBody Map<String, String> map) {
        String orderId = map.get("orderId");
        FlightOrderQt orderQt = flightOrderQtService.flightOrderQt(orderId);
        return AjaxResult.success(orderQt);
    }

    /**
     * 处理延误信息
     * @param flightOrderQt
     * @return
     */
    @PostMapping("/updateOrderQt")
    public AjaxResult updateOrderQt(HttpServletRequest request, @RequestBody FlightOrderQt flightOrderQt) {
        String token = request.getHeader("token");
        R<Employee> employeeByToken;
        try {
            employeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (employeeByToken.getCode() == 200 && employeeByToken.getData() != null) {
            return toAjax(flightOrderQtService.updateOrderQt(employeeByToken.getData(), flightOrderQt));
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }
}
